layui.use(['table', 'util', 'layer', 'laydate'], function(){
    var table = layui.table,
        util = layui.util,
        layer = layui.layer,
        laydate = layui.laydate;

    laydate.render({
        elem: '.uBirthday'
    });

    var userTable = table.render({
        elem: '#testTable'
        ,url:'/admin/selectByPage'
        ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        ,title: '用户数据表'
        ,method: 'get'
        ,limits: [10,15,20,30]
        ,limit: 10
        ,id:'testTable'
        ,height: 523
        ,cellMinWidth: 80
        ,cols: [[
            {field:'id', title:'序号', align:'center',type: 'numbers', width: 100, unresize: true, sort: true}
            ,{field:'uaccount', title:'用户名',align:'center'}
            ,{field:'uname', title:'姓名',align:'center'}
            ,{field:'usex', title:'性别',align:'center', templet: function (res) {
                    return res.usex == "0" ? "男" : "女";
                }}
            ,{field:'ubirthday', title:'出生日期',align:'center',width:160, templet: function (res) {
                    return util.toDateString(res.ubirthday, "yyyy-MM-dd");
                }}
            ,{field:'uico', title:'头像',align:'center',templet: function (res) {
                    return res.uico == null ? "" : "<img src='"+res.uico+"' style='width: 50px;height: 30px' />";
                }}
            ,{field:'uphone', title:'手机',align:'center'}
            ,{field:'uemail', title:'邮箱',align:'center',width:170}
            ,{field:'urole', title:'角色',align:'center', templet: function (res) {
                    return res.urole == "0" ?
                        "<a class='layui-btn layui-btn-primary layui-btn-xs'>学生</a>"
                        : "<a class='layui-btn layui-btn-normal layui-btn-xs'>管理员</a>";
                }}
            ,{field:'ustatus', title:'状态',align:'center', templet: function (res) {
                    return res.ustatus == "0" ? "<span style='color: #e1e1e1;'>未启用</span>"
                        : "启用";
                }}
            ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo' ,width:230 }
        ]]
        ,page: true
        ,response: {
            "code": 0,
            "msg": "",
            "count": 1000,
            "data": []
        }
        ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.data.code, //解析接口状态
                "msg": res.data.msg, //解析提示文本
                "count": res.data.count, //解析数据长度
                "data": res.data.data //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(test)', function(obj){
        //获取选中的表单元素   需配合单选 多选 按钮
        switch(obj.event){
            case 'addUser':
                //获取选中的表单元素内容
                layer.open({
                    type: 2,
                    area: ['455px', '590px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/addUser'
                });
                break;
            case 'reloadPage':
                var pageIndex = $(".layui-laypage-em").next().html();
                userTable.reload({
                    method:'get'
                    ,page: {
                        curr: pageIndex //重新从第 当前页 开始
                    }
                });
                break;
        }
    });

    $('.do_search').on('click', function () {
        // 搜索条件
        var uAccount=$(".uAccount").val();
        var uName=$(".uName").val();
        var uSex=$(".uSex").val();
        var uPhone=$(".uPhone").val();
        var uBirthday=$(".uBirthday").val();
        var uRole=$(".uRole").val();
        var uStatus=$(".uStatus").val();
        userTable.reload({
            method:'get'
            ,where: { //设定异步数据接口的额外参数，任意设
                uAccount: uAccount,
                uName: uName,
                uSex: uSex,
                uPhone: uPhone,
                uBirthday: uBirthday,
                uRole: uRole,
                uStatus: uStatus
            }
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    });

    //监听行工具事件
    table.on('tool(test)', function(obj){
        var data = obj.data;
        if(obj.event === 'ban'){
            var status = "禁用"
                ,ustatus = "0";
            if (data.ustatus === '0'){
                status = "启用";
                ustatus = "1";
            }
            layer.confirm('是否<b style="color:red">' + status + '</b>用户：<span style="color:blueviolet">' + data.uname + '</span>', function(index){
                //获取table表格当前页码
                var pageIndex = $(".layui-laypage-em").next().html();
                $.ajax({
                    url:"/admin/updateStatus/" + data.uid + "/" + ustatus,
                    type:"put",
                    data:'',
                    success:function (result) {
                        if (result.code == 200) {
                            obj.del();
                            layer.close(index);
                            userTable.reload({
                                method:'get'
                                ,page: {
                                    curr: pageIndex //重新从当前页开始
                                }
                            });
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                });
            });
        } else if(obj.event === 'edit'){
            layer.open({
                type: 2,
                area: ['455px', '590px'],
                fixed: false, //不固定
                maxmin: true,
                content: ['/updateOther/' + data.uid, 'no']
            });
        } else if (obj.event === 'reset'){
            layer.confirm('是否重置密码？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    url:"/admin/resetPwd/" + data.uid,
                    type:"put",
                    data:'',
                    success:function (res) {
                        if (res.code == 200) {
                            layer.msg(res.msg, {icon: 1}, {
                                time: 5000
                            });
                        } else {
                            layer.msg(res.msg, {icon: 2}, {
                                time: 5000
                            });
                        }
                    }
                });
            });
        }else if(obj.event === 'updateRole'){
            var role = "管理员"
                ,urole = "1";
            if (data.urole === '1'){
                role = "学生";
                urole = "0";
            }
            layer.confirm('是否将：<span style="color:blueviolet">' + data.uname + '</span>' + '设置为 <span style="color:deeppink">' + role + '</span>', function(index){
                //获取table表格当前页码
                var pageIndex = $(".layui-laypage-em").next().html();
                $.ajax({
                    url:"/admin/updateRole/" + data.uid + "/" + urole,
                    type:"put",
                    data:'',
                    success:function (result) {
                        if (result.code == 200) {
                            layer.close(index);
                            userTable.reload({
                                method:'get'
                                ,page: {
                                    curr: pageIndex //重新从当前页开始
                                }
                            });
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                });
            });
        }
    });

    var _tools = {
        reloadTable : function () {
            var pageIndex = $(".layui-laypage-em").next().html();
            userTable.reload({
                method:'get'
                ,page: {
                    curr: pageIndex //重新从第 当前页 开始
                }
            });
        }
    };
    window.tools = _tools;
});